package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import GuiUtils.SingleField;

import net.miginfocom.swing.MigLayout;
import zadanie.Zadanie;

public class ZadanieEdit extends JFrame {
    
    private int id;
    private Zadanie zadanie;
    private MigLayout layout;
    
    private JLabel lId;
    private JLabel l2Id;
    private JLabel lTytul;
    private JScrollPane l2Tytul;
    private JLabel lOpis;
    private JTextArea l2Opis;
    private JTextArea textArea ;
    private JLabel lTermin;
    private JTextArea l2Termin;
    private JLabel lPriorytet;
    private JComboBox prirList;
    private JButton zapisz;
    
    public ZadanieEdit(int nr, JPanel oneTaskRow){
        id = nr;
        
        this.setTitle("Zadanie numer: " + id);
        zadanie = Zadanie.pobierzZadanie(id);
        layout = new MigLayout();
        
        this.setLayout(layout);
        
        initComponents(oneTaskRow);
        this.setVisible(true);
        this.setSize(500,250);
        this.setResizable(false);
        this.setAlwaysOnTop(true);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension ekran = toolkit.getScreenSize();
        setLocation((ekran.width-500)/2, (ekran.height-250)/2); 
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
        
        
    }
    
    private void initComponents(final JPanel oneTaskRow){      
            
        lId = new JLabel("Id: ");
        l2Id = new JLabel(Integer.toString(zadanie.pokazId()));
        lId.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        l2Id.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        this.add(lId);this.add(l2Id, "wrap");
       
        
        lTytul = new JLabel("Nazwa: ");
        textArea = new JTextArea(2, 40);
        textArea.setText(zadanie.pokazTytul());
        textArea.setLineWrap(true);
        
        l2Tytul = new JScrollPane( textArea );
        lTytul.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        textArea.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        this.add(lTytul);this.add(l2Tytul, "wrap");
        
        
        
        
        lOpis = new JLabel("Opis: ");
       
        l2Opis = new JTextArea(2,40);
        l2Opis.setText(zadanie.pokazOpis());
        l2Opis.setLineWrap(true);
        JScrollPane lOp = new JScrollPane(l2Opis);
        lOpis.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        l2Opis.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        this.add(lOpis);this.add(lOp, "wrap");        
        
              
        lTermin = new JLabel("Termin: ");
        DateFormat formatter2 ; 
        Date date2 ; 
        formatter2 = new SimpleDateFormat("yyyy-MM-d");
        date2 = zadanie.pokazTermin();
        String s2 = formatter2.format(date2);        
        l2Termin = new JTextArea(1,6);
        l2Termin.setText(s2);
        JScrollPane dat = new JScrollPane(l2Termin);
        
       
        lTermin.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        l2Termin.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        this.add(lTermin);this.add(dat, "wrap");        
        
        lPriorytet = new JLabel("Priorytet: ");
        
        String[] prior = { "1", "2", "3" };
        
       int i = zadanie.pokazStatus();
       prirList = new JComboBox(prior);
        prirList.setSelectedIndex(i-1);
        
        
        lPriorytet.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        prirList.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
       
        
      
        zapisz = new JButton("Zapisz");
        zapisz.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        this.add(lPriorytet);this.add(prirList);this.add(zapisz,"wrap");
        zapisz.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                zadanie.ustawTytul(textArea.getText());
                zadanie.ustawOpis(l2Opis.getText());
                zadanie.ustawPriorytet(prirList.getSelectedIndex()+1);
                DateFormat dateFormat = new SimpleDateFormat("yyyy-M-D");
                
                try {
                    Date convertedDate =dateFormat.parse(l2Termin.getText());
                    java.sql.Date mySqlDate = new java.sql.Date(convertedDate.getTime()); 
                    zadanie.ustawTermin(mySqlDate);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                    
                JLabel title =(JLabel) oneTaskRow.getComponent(1);
                title.setText(textArea.getText());
                PageZadanie.refreshTasks();
                        dispose();    
            }
            
        });
       
    }    

}
